import Vue from 'vue'
import App from './App.vue'
// 路由
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

import Meta from 'vue-meta'
Vue.use(Meta)

import sha1 from 'js-sha1'
Vue.prototype.sha1 = sha1
// Vue.prototype.$sha1Secret = 'OK6vff3Rckog6Ye7'
// 标签样式重置
import './assets/reset.css'

//复制到粘贴板插件
import VueClipboard from 'vue-clipboard2'
VueClipboard.config.autoSetContainer = true

Vue.use(VueClipboard)

//滚动轮播
import scroll from 'vue-seamless-scroll'
Vue.use(scroll)

//接口请求封装
import http from './http.js'
Vue.prototype.http = http

// RSA加密
import { JSEncrypt } from 'jsencrypt'
Vue.prototype.$getCode = (params) => {
    return new Promise((resolve) => {
        let encrypt = new JSEncrypt()
        encrypt.setPrivateKey('-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvF5p84FCcNfAD9VmQ93IZa9XnyWtYmGor5CVA3MKr4+soQ1W1AxQ7WAS3xabalGKSpbHV4r/Vc+gLbUrUE1LnrTxQInq1BWgQkxlmSa4VJx5SYIfyTnSp73WkFWYYgtR7TP+4yelVeCmTCOh5xXOe30Rs67IX2wt1BSWOPNdg6TnddUpmnZ0so3mZ/kkUEIAkGlPVfHVZhzckuGB9suibf/PsJBVy34TqbLxRy1JWj4W08YeCcXnS9UD1wmKnh1v5OOLnc+AvhUz+hpfZ23B7PVrO519CMftXVgdeBD0CdA+dlllmrN7IGRVgxqgcO2pBXrCnkDEKtt5XTnxKtuF1IS0vzlNPGkcEherPkCxV+d/p0JMU/yBTHLkKr2oRPdqAfPnVOKW2k8tAoQPRkEVQ1janzntRC6V2sSmsqu1OWMW6GGlbopIvXwKGr31JRtUzbnrp0Pt8sIXMYTTFfJILi3A7OFm5gkVGJ+TGe0GbH63v2DsiN1k9W53wqbBbl3LWf1oOqmihaS1XLUvlOY4YUBz2QmH8snsjeGhdU+rMMfSmBxermjpWvICyOZm/aO9XETLCAm3n8/SGQ8F63sTaVijlwICmkPMr4qu5lctNJrVXmpDSjMVCLqi71qliBRu/7YwvAbOt+05ceWgjQurtgXCuuE3SMRGDF/scnE+jyMCAwEAAQ==-----END PUBLIC KEY-----')
        let data = encrypt.encrypt(params)
        //console.log(params)
        resolve(data)
    })
}

// 排序
Vue.prototype.$strSort = (params) => {
    return new Promise((resolve) => {
        // console.log('params', params)
        let arr = []
        arr.push(params.split('&'))
        let arr2 = []
        // console.log(arr)
        for (let i = 0; i < arr[0].length; i++) {
            // console.log(arr[0])
            arr2.push({
                index: i,
                str: arr[0][i].split("=")
            })
        }
        // console.log(arr2)
        arr2.sort((a, b) => {
            return a.str[0].localeCompare(b.str[0], 'zh-CN')
        })
        let str = ""
        arr2.forEach((el) => {
            // console.log(index, el)
            str = str === "" ? arr[0][el.index] : str + '&' + arr[0][el.index]
        })
        resolve(str)
    })
}

Vue.prototype.$getSystem = () => {
    return new Promise(resolve => {
        let rsaStr = `timestamp=${Number(new Date())}`
        let data = null;
        Vue.prototype.$getCode(rsaStr).then(rsaCode => {
            Vue.prototype.http.getSystem(rsaCode).then(res => {
                data = res.data.result.data
                if (res.data.errno === "000") {
                    resolve(data)
                } else {
                    data = null
                    resolve(data)
                }
            })
        })

    })
}

router.afterEach((to) => {
    /* 路由发生变化修改页面 title */
    if (to.meta.title) {
        document.title = to.meta.title
    } else {
        document.title = '全能猫'
    }
    // next()
})


Vue.config.productionTip = false

new Vue({
    router,
    render: h => h(App),
}).$mount('#app')
